from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from backend.models import Course, KidCourse


# 获取成人课程数据接口
@csrf_exempt
def course_info(request):
    response = {}
    try:
        course_name = request.GET.get('course_name')
        teacher_name = request.GET.get('course_teacher')
        cid = request.GET.get('id')
        if course_name:
            if teacher_name:
                info = Course.objects.filter(course_name=course_name, course_teacher=teacher_name)
            else:
                info = Course.objects.filter(course_name=course_name)
        elif cid:
            info = Course.objects.filter(id=cid)
        else:
            info = Course.objects.all()
        response['list'] = list(info.values())
        response['respMsg'] = 'success'
        response['respCode'] = '000000'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 成人课程新增接口
@csrf_exempt
def course_add(request):
    response = {}
    try:
        name = request.GET.get('name')
        course_teacher = request.GET.get('course_teacher')
        startDate = request.GET.get('startDate')
        endDate = request.GET.get('endDate')
        open_status = request.GET.get('open')
        filepath = request.GET.get('filepath')
        filename = request.GET.get('filename')
        Course.objects.create(course_name=name, course_teacher=course_teacher, course_startDate=startDate,
                              course_endDate=endDate, course_status=open_status, course_fengmian=filepath,
                              course_imgName=filename)
        response['respMsg'] = 'success'
        response['respCode'] = '000000'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 成人删除课程接口
@csrf_exempt
def course_del(request):
    response = {}
    try:
        id = request.GET.get('id')
        Course.objects.filter(id=id).delete()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 成人课程编辑接口
@csrf_exempt
def course_edit(request):
    response = {}
    try:
        cid = request.GET.get('id')
        name = request.GET.get('name')
        course_teacher = request.GET.get('course_teacher')
        startDate = request.GET.get('startDate')
        endDate = request.GET.get('endDate')
        open_status = request.GET.get('open')
        filepath = request.GET.get('filepath')
        filename = request.GET.get('filename')
        Course.objects.filter(id=cid).update(course_name=name, course_teacher=course_teacher,
                                             course_startDate=startDate,
                                             course_endDate=endDate, course_status=open_status,
                                             course_fengmian=filepath, course_imgName=filename)
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 获取少儿课程数据接口
@csrf_exempt
def kid_course_info(request):
    response = {}
    try:
        course_name = request.GET.get('course_name')
        teacher_name = request.GET.get('course_teacher')
        cid = request.GET.get('id')
        if course_name:
            if teacher_name:
                info = KidCourse.objects.filter(course_name=course_name, course_teacher=teacher_name)
            else:
                info = KidCourse.objects.filter(course_name=course_name)
        elif cid:
            info = KidCourse.objects.filter(id=cid)
        else:
            info = KidCourse.objects.all()
        response['list'] = list(info.values())
        response['respMsg'] = 'success'
        response['respCode'] = '000000'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 少儿课程新增接口
@csrf_exempt
def kid_course_add(request):
    response = {}
    try:
        name = request.GET.get('name')
        course_teacher = request.GET.get('course_teacher')
        startDate = request.GET.get('startDate')
        endDate = request.GET.get('endDate')
        open_status = request.GET.get('open')
        filepath = request.GET.get('filepath')
        filename = request.GET.get('filename')
        KidCourse.objects.create(course_name=name, course_teacher=course_teacher, course_startDate=startDate,
                                 course_endDate=endDate, course_status=open_status, course_fengmian=filepath,
                                 course_imgName=filename)
        response['respMsg'] = 'success'
        response['respCode'] = '000000'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 少儿删除课程接口
@csrf_exempt
def kid_course_del(request):
    response = {}
    try:
        id = request.GET.get('id')
        KidCourse.objects.filter(id=id).delete()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


# 少儿课程编辑接口
@csrf_exempt
def kid_course_edit(request):
    response = {}
    try:
        cid = request.GET.get('id')
        name = request.GET.get('name')
        course_teacher = request.GET.get('course_teacher')
        startDate = request.GET.get('startDate')
        endDate = request.GET.get('endDate')
        open_status = request.GET.get('open')
        filepath = request.GET.get('filepath')
        filename = request.GET.get('filename')
        KidCourse.objects.filter(id=cid).update(course_name=name, course_teacher=course_teacher,
                                                course_startDate=startDate,
                                                course_endDate=endDate, course_status=open_status,
                                                course_fengmian=filepath, course_imgName=filename)
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)